<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\AgricGoodsTable;
use App\Admin\Renderable\ArticleTable;
use App\Admin\Renderable\PaperLibraryTable;
use App\Admin\Renderable\VillagePublishTable;
use App\Admin\Repositories\AdvContent;
use App\Admin\Repositories\AgricGood;
use App\Admin\Repositories\Article;
use App\Admin\Repositories\PaperLibrary;
use App\Admin\Repositories\VillagePublish;
use App\Models\AdvType;
use App\Models\AgricGoods;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Support\Helper;

class AdvContentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    public static function grid($advTypeId = 0)
    {
        return Grid::make(new AdvContent(), function (Grid $grid) use ($advTypeId) {
            if ($advTypeId) {
                $grid->model()->where('adv_type_id', $advTypeId);

                // 如果该广告位限制地区，则只看该地区下的内容
                $region_id = Admin::user()->region_id;
                if ($region_id) {
                    $advType = AdvType::find($advTypeId);
                    if ($advType->is_region) {
                        $grid->model()->where('region_id', $region_id);
                    }
                }
            }

            $grid->column('id')->sortable();
            $grid->column('adv_name');
            $grid->column('adv_img')->image('', 60, 60);
            $grid->column('adv_img_pc', 'PC端图片')->image('', 60, 60);
            $grid->column('obj_id', '关联内容')->display(function ($obj_id) {
                // 类型（1文章；2调查问卷；3地址;4商品）
                if ($this->adv_type == 1) {
                    $obj = \App\Models\Article::find($obj_id);
                    return $obj ? $obj->article_title : '';
                } else if ($this->adv_type == 2) {
                    $obj = \App\Models\PaperLibrary::find($obj_id);
                    return $obj ? $obj->paper_title : '';
                } else if ($this->adv_type == 3) {
                    $pageName = \App\Admin\Repositories\AdvType::getAdvUrlList();
                    if (isset($pageName[$this->url])) {
                        return $pageName[$this->url];
                    } else {
                        return '';
                    }
                } else if ($this->adv_type == 4) {
                    $obj = AgricGoods::find($obj_id);
                    return $obj ? $obj->goods_name : '';
                }  else if ($this->adv_type == 5) {
                    $obj = \App\Models\VillagePublish::find($obj_id);
                    return $obj ? $obj->pub_title : '';
                } else {
                    return '';
                }
            });
            $grid->column('sort')->sortable();
            $grid->column('is_show')->switch()->sortable();
//            $grid->column('created_at');
//            $grid->column('adv_type_id');
//            $grid->column('adv_type');
//            $grid->column('updated_at')->sortable();

            $grid->disableViewButton(); // 禁用详情按钮
            $grid->quickSearch(['adv_name']);
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('is_show', '展示状态', [0 => '已隐藏', 1 => '已显示']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public static function detail($id)
    {
        return Show::make($id, new AdvContent(), function (Show $show) {
            $show->field('id');
            $show->field('adv_type_id');
            $show->field('adv_name');
            $show->field('adv_img');
            $show->field('adv_type');
            $show->field('obj_id');
            $show->field('sort');
            $show->field('is_show');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public static function form($advTypeId = 0)
    {
        return Form::make(new AdvContent(), function (Form $form) use ($advTypeId) {

            $form->display('id');
//            $advType = AdvType::find($advTypeId);
//            $form->text('adv_type_name')->value($advType->adv_type_name)->disable();
            $form->text('adv_name', '广告位名称')->required();
            $form->image('adv_img')->move('adv/'.date('Y-m-d'))->autoUpload();
            $form->image('adv_img_pc', 'PC端图片')->move('adv/'.date('Y-m-d'))->autoUpload();
            $region_id = Admin::user()->region_id;

            $obj_id1 = 0;
            $obj_id2 = 0;
            $obj_id3 = 0;
            if ($form->model()->adv_type == 1) {
                $obj_id1 = $form->model()->obj_id;
            }
            if ($form->model()->adv_type == 2) {
                $obj_id2 = $form->model()->obj_id;
            }
            if ($form->model()->adv_type == 4) {
                $obj_id3 = $form->model()->obj_id;
            }

            // 文章类型（1图文，2视频，3短视频）
            $typeOptions = [1 => '文章', 2 => '调查问卷', 3 => 'APP页面', 4 => '商品', 5 => '农品助力',6=>'文字诵读'];
            $form->radio('adv_type')
                ->when([1], function (Form $form) use($region_id, $advTypeId, $obj_id1) {

                    $form->selectTable('obj_id1', '文章选择')
                        ->title('文章列表')
                        ->from(ArticleTable::make(['adv_type_id' => $advTypeId, 'region_id' => $region_id,'type'=>'1,3']))
                        ->model(\App\Models\Article::class, 'id', 'article_title')
//                        ->options(function ($v) {
//                            if (!$v) {
//                                return [];
//                            }
//                            return \App\Models\Article::find($v)->pluck('article_title as name', 'id');
//                        })
                        ->placeholder('请点击右侧箭头选择当前模块下的文章')
                        ->value($obj_id1);

                })
                ->when([2], function (Form $form) use($region_id, $obj_id2) {
                    $form->selectTable('obj_id2', '问卷调查')
                        ->title('问卷调查')
                        ->from(PaperLibraryTable::make(['region_id' => $region_id]))
                        ->model(\App\Models\PaperLibrary::class, 'id', 'paper_title')
                        ->placeholder('请点击右侧箭头选择问卷调查'.$form->obj_id)
                        ->value($obj_id2);
                })
                ->when([3], function (Form $form) {
                    $form->select('url', '跳转页面')
                        ->options(\App\Admin\Repositories\AdvType::getAdvUrlList())
                        ->default(0, true);
                })
                ->when([4], function (Form $form) use($region_id, $obj_id3) {
                    $form->selectTable('obj_id3', '商品选择')
                        ->title('商品列表')
                        ->from(AgricGoodsTable::make(['region_id' => $region_id]))
                        ->model(\App\Models\AgricGoods::class, 'id', 'goods_name')
                        ->placeholder('请点击右侧箭头选择商品')
                        ->value($obj_id3);
                })
                ->when([5], function (Form $form) use($region_id, $obj_id3) {
                    $form->selectTable('obj_id4', '农品助力选择')
                        ->title('农品助力列表')
                        ->from(VillagePublishTable::make(['region_id' => $region_id]))
                        ->model(\App\Models\VillagePublish::class, 'id', 'pub_title')
                        ->placeholder('请点击右侧箭头选择内容')
                        ->value($obj_id3);
                })
				 ->when([6], function (Form $form) use($region_id, $advTypeId, $obj_id1) {

                    $form->selectTable('obj_id5', '文章选择')
                        ->title('文章列表')
                        ->from(ArticleTable::make(['adv_type_id' => $advTypeId, 'region_id' => $region_id,'type'=>4]))
                        ->model(\App\Models\Article::class, 'id', 'article_title')
                        ->placeholder('请点击右侧箭头选择当前模块下的文章')
                        ->value($obj_id1);

                })
                ->options($typeOptions)
                ->required()
                ->default(1);

            $form->number('sort')->value(1);
            $form->radio('is_show', '状态')->options([1 => '显示', 0 => '隐藏'])->default(1);
            $form->hidden('adv_type_id')->value($advTypeId);
            $form->hidden('obj_id');
            $form->hidden('region_id')->value($region_id);
            $form->display('created_at');
            $form->display('updated_at');


            $form->saving(function ($form) {
                if ($form->adv_type == 1) {
                    $form->obj_id = $form->obj_id1;
                }
                if ($form->adv_type == 2) {
                    $form->obj_id = $form->obj_id2;
                }
                if ($form->adv_type == 4) {
                    $form->obj_id = $form->obj_id3;
                }
                if ($form->adv_type == 5) {
                    $form->obj_id = $form->obj_id4;
                }
				if ($form->adv_type == 6) {
                    $form->obj_id = $form->obj_id5;
                }
                $form->region_id = Admin::user()->region_id;
                $form->deleteInput('obj_id1');
                $form->deleteInput('obj_id2');
                $form->deleteInput('obj_id3');
                $form->deleteInput('obj_id4');
				$form->deleteInput('obj_id5');
            });

        });
    }
}
